/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.boundary;

import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Users;
import it.polimi.registration.business.security.entity.UsersEvent;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 *
 * @author Luca
 */
@Stateless
public class UsersEventManager {
    @PersistenceContext
    EntityManager em;

    @Inject
    Principal principal;
    
    public void save(UsersEvent usersEvent) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<UsersEvent>> constraintViolations = validator.validate(usersEvent);
        if (constraintViolations.size() > 0) {
            Iterator<ConstraintViolation<UsersEvent>> iterator = constraintViolations.iterator();
            while (iterator.hasNext()) {
                ConstraintViolation<UsersEvent> cv = iterator.next();
                System.err.println(cv.getRootBeanClass().getName() + "." + cv.getPropertyPath() + " " + cv.getMessage());

                //JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName() + "." + cv.getPropertyPath() + " " + cv.getMessage());
            }
        } else {
            em.persist(usersEvent);
        }
    }

    public void delete() {
        em.remove(getSelectedEvent());
    }

    private Object getSelectedEvent() {
        return em.find(UsersEvent.class, principal.getName());
    }
    
    public Users findUsersByEmail(String email){
        Query query = em.createQuery("SELECT u FROM Users u WHERE u.email= :email").setParameter("email", email);
        Users loadedUsers = (Users)query.getSingleResult();
        return loadedUsers;
    }

    public void accept(Event event, Users users) {
        Query query = em.createQuery("UPDATE UsersEvent ue SET ue.accepted=true , ue.seen=true WHERE (ue.event= :eventToUpdate AND ue.users= :usersToUpdate)");
        query.setParameter("eventToUpdate", event);
        query.setParameter("usersToUpdate", users);
        query.executeUpdate();
        
    }
    
    public void decline(Event event, Users users){
        Query query = em.createQuery("UPDATE UsersEvent ue SET ue.seen=true , ue.seen=true WHERE (ue.event= :eventToUpdate AND ue.users= :usersToUpdate)");
        query.setParameter("eventToUpdate", event);
        query.setParameter("usersToUpdate", users);
        query.executeUpdate();
    }
    
    public boolean isCreator(Event event , Users users)
    {
          Query query = em.createQuery("Select ue From UsersEvent ue where (ue.event.id= :event AND ue.users= :users)");
               query.setParameter("event", event.getId());
               query.setParameter("users", users);
               
               List<UsersEvent> result = new ArrayList<>(query.getResultList());
               return result.get(0).getCreator();
    }
    
    public void removeAllInvitationToAnEvent(Event event){
        Query query1 = em.createQuery("SELECT ue FROM UsersEvent ue WHERE ue.event.id=:event").setParameter("event", event.getId());
        List<UsersEvent> queryResult = new ArrayList<>(query1.getResultList());
        for (int i = 0; i < queryResult.size(); i++){
            Query query=em.createQuery("DELETE FROM UsersEvent ue WHERE ue.id= :id").setParameter("id", queryResult.get(i).getId());
            query.executeUpdate();
        }
        
    }
    
    
    
}
